﻿using System.IO;
using System.Web;
using System.Web.Mvc;
using PSTUTemplate.Models;
using PSTUTemplate.Models.Object;
using PSTUTemplate.Models.Object.ViewModels;
using PSTUTemplate.Models.DBObject;


namespace PSTUTemplate.Controllers
{
    public class ExamController : Controller
    {
        Repository rep = new Repository();
        //
        // GET: /Exam/

        public ActionResult Index()
        {
            return View();
        }


        //// GET: /Exam/Details/5


        public ActionResult Details()
        {
            string loggeduser = User.Identity.Name;
            ExamDisplayModel viewModels = new ExamDisplayModel();
            
            viewModels.Profiles = rep.GetProfileByLoggedUserName(loggeduser);
            viewModels.Enrols.StudentEnrols =rep.GetAStudentEnrolById(viewModels.Profiles.UserId);


            return View(viewModels);
        }

        [HttpPost]
        public ActionResult Details(ExamDisplayModel viewModels, HttpPostedFileBase file)
        {
            Repository rep = new Repository();
            string loggeduser = User.Identity.Name;
            viewModels.Profiles = rep.GetProfileByLoggedUserName(loggeduser);

            // TODO: Add insert logic here
            string fileName = "";

            if (file.ContentLength > 0)
            {
                fileName = Path.GetFileName(file.FileName);
                var Extension = fileName.Substring(fileName.LastIndexOf('.') + 1).ToLower();
                fileName = fileName + "_" + viewModels.Profiles.Name + "_" + viewModels.Profiles.Phone + "." + Extension;
                var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
            }
            

            viewModels.MarkSheet.ImageAvatar = fileName;

            return View(viewModels);
        }

        //
        // GET: /Exam/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Exam/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Exam/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Exam/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        [HttpGet]
        public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {

            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
            }

            return View();
        }

    }
}
